package com.java.Servlet;

import com.java.Exception.MyException;
import com.java.entity.Admins;
import com.java.entity.Orders;
import com.java.entity.Users;
import com.java.service.AdminService;
import com.java.service.Impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
    AdminService service = new AdminServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8"); //处理请求的乱码
        resp.setCharacterEncoding("UTF-8"); //处理响应的乱码
        String mark = req.getParameter("mark");
        //登录操作
        if ("AdminLogin".equals(mark.trim())) {
            this.doAdminLogin(req, resp);
        } else if ("ListOrder".equals(mark.trim())) {
            this.doListOrder(req, resp);
        } else if ("checkOrderByBookId".equals(mark.trim())) {
            this.doCheckOrderByBookId(req, resp);
        } else if ("updateOrderById".equals(mark.trim())) {
            this.doUpdateOrderById(req, resp);
        } else if ("ListUsers".equals(mark.trim())) {
            this.doListUsers(req, resp);
        } else if ("checkUserById".equals(mark.trim())) {
            this.doCheckUserById(req, resp);
        } else if ("updateUserById".equals(mark.trim())) {
            this.doUpdateUserById(req, resp);
        } else if ("adminList".equals(mark.trim())) {
            this.doAdminList(req, resp);
        } else if ("checkAdminById".equals(mark.trim())) {
            this.doCheckAdminById(req, resp);
        } else if ("updateAdminById".equals(mark.trim())) {
            this.doUpdateAdminById(req, resp);
        }
        else if ("addAdmin".equals(mark)){
            this.doAddAdmin(req,resp);
        }
    }

    private void doAddAdmin(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        try {
            boolean flag = service.addAdmin(id,username, password);
            if(flag){
                req.getRequestDispatcher("/admin/adminList.jsp").forward(req,resp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

        private void doUpdateAdminById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Admins admin = new Admins();
        admin.setPassword(req.getParameter("password"));
        admin.setUserName(req.getParameter("userName"));
        admin.setAdminId(req.getParameter("id"));
            try {
                boolean flag = service.updateAdminById(admin);
                if (flag)
                    this.doAdminList(req,resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        private void doCheckAdminById (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String adminId = req.getParameter("id");
            try {
                Admins admins = service.checkAdminById(adminId);
                req.getSession().setAttribute("admin",admins);
                req.getRequestDispatcher("/admin/reAdmin.jsp").forward(req,resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        private void doAdminList (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
        {
            try {
                List<Admins> admins = service.selectAllAdmins();
                req.getSession().setAttribute("admins", admins);
                req.getRequestDispatcher("/admin/adminList.jsp").forward(req, resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        private void doUpdateUserById (HttpServletRequest req, HttpServletResponse resp) throws
        ServletException, IOException {
         /* private String name;//年龄
                private String  sex;//性别
                private int age;//年龄
                private String id;//身份证号码
                private String phone;//手机号码
                private String address;//家庭住址
                private String userid;//账号 用来登录
                private String password;//密码 用来登录*/
            Users users = new Users();
            users.setId(req.getParameter("id"));
            users.setAddress(req.getParameter("address"));
            users.setAge(Integer.parseInt(req.getParameter("age")));
            users.setName(req.getParameter("name"));
            users.setPassword(req.getParameter("password"));
            users.setPhone(req.getParameter("phone"));
            users.setSex(req.getParameter("sex"));
            users.setUserid(req.getParameter("userid"));
            try {
                boolean flag = service.updateUserById(users);
                if (!flag) {
                    throw new MyException("数据库错误");
                }
                this.doListUsers(req, resp);
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (MyException e) {
                e.printStackTrace();
            }
        }



        private void doCheckUserById (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String id = req.getParameter("id");
            try {
                Users users = service.selectUserById(id);
                req.getSession().setAttribute("user", users);
                req.getRequestDispatcher("/admin/reUsers.jsp").forward(req, resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        private void doListUsers (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
        {
            try {
                List<Users> users = service.selectAllUsers();
                req.getSession().setAttribute("users", users);
                req.getRequestDispatcher("/admin/用户列表.jsp").forward(req, resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        private void doUpdateOrderById (HttpServletRequest req, HttpServletResponse resp) throws
        ServletException, IOException {
            Orders orders = new Orders();
            String bookId = req.getParameter("bookId");
            String name = req.getParameter("name");
            int age = Integer.parseInt(req.getParameter("age"));
            String sex = req.getParameter("sex");
            String address = req.getParameter("address");
            String id = req.getParameter("id");
            String phone = req.getParameter("phone");
            String origin = req.getParameter("origin");
            String destination = req.getParameter("destination");
            int ticketprice = Integer.parseInt(req.getParameter("ticketprice"));
            orders.setAddress(address);
            orders.setAge(age);
            orders.setBookId(bookId);
            orders.setName(name);
            orders.setSex(sex);
            orders.setId(id);
            orders.setPhone(phone);
            orders.setOrigin(origin);
            orders.setDestination(destination);
            orders.setTicketPrice(ticketprice);
            System.out.println(orders);
            try {
                boolean flag = service.UpdateOrderById(orders);
                if (!flag) {
                    throw new MyException("数据库错误");
                }
                this.doListOrder(req, resp);
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (MyException e) {
                e.printStackTrace();
            }
        }

        private void doCheckOrderByBookId (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String bookId = req.getParameter("bookId");
            try {
                Orders orders = service.selectOrdersByBookId(bookId);
                req.getSession().setAttribute("orders", orders);
                req.getRequestDispatcher("/admin/reorder.jsp").forward(req, resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        private void doListOrder (HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException
        {
            try {
                List<Orders> orderList = service.selectAllOrder();
                req.getSession().setAttribute("orders", orderList);
//            for (int i= 0 ; orderList.size()> i ;i++) {
//                for (Orders orders:orderList){
//                    System.out.println(orders);
//                }
//            }
                req.getRequestDispatcher("/admin/订单信息列表.jsp").forward(req, resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        private void doAdminLogin (HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
            String userName = req.getParameter("userName");
            String password = req.getParameter("password");
            try {
                Admins admin = service.selectAdminByUserNameAndPassword(userName, password);
                if (admin == null || admin.equals("")) {
                    throw new MyException("账号或密码输入错误");
                } else {
                    req.getSession().setAttribute("admin", admin);
                    req.getRequestDispatcher("/admin/index.jsp").forward(req, resp);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (MyException e) {
                e.printStackTrace();
                req.getSession().setAttribute("msg", e.getMessage());
                resp.sendRedirect(req.getContextPath() + "/admin/login.jsp");
            }
        }

}
